package com.sofeteem.ikun.view;

import com.sofeteem.ikun.utils.DBUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RegFrame extends JFrame {
    public RegFrame() {
        // 设置窗口标题
        setTitle("王牌空战-角色注册");
        // 设置窗口大小
        setSize(400, 300); // 调整窗口大小
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口居中显示
        setLocationRelativeTo(null);
        // 设置布局管理器
        setLayout(new BorderLayout());

        // 创建主面板并设置布局
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20)); // 设置边距

        // 标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel titleLabel = new JLabel("王牌空战-角色注册", SwingConstants.CENTER);
        // 设置标题字体大小
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titlePanel.add(titleLabel);

        // 输入框面板
        JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 用户名输入框
        JLabel usernameIcon = new JLabel(new ImageIcon("resources/imgs/username.png")); // 替换为实际路径
        JTextField usernameField = new JTextField("请输入账号", 20);
        usernameField.setForeground(Color.GRAY);
        usernameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                if (usernameField.getText().equals("请输入账号")) {
                    usernameField.setText("");
                    usernameField.setForeground(Color.BLACK);
                }
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                if (usernameField.getText().isEmpty()) {
                    usernameField.setText("请输入账号");
                    usernameField.setForeground(Color.GRAY);
                }
            }
        });

        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(usernameIcon, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        inputPanel.add(usernameField, gbc);

        // 密码输入框
        JLabel passwordIcon = new JLabel(new ImageIcon("resources/imgs/password.png")); // 替换为实际路径
        JPasswordField passwordField = new JPasswordField("请输入密码", 20);
        passwordField.setForeground(Color.GRAY);
        passwordField.setEchoChar((char) 0); // 先不显示密码
        passwordField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                if (String.valueOf(passwordField.getPassword()).equals("请输入密码")) {
                    passwordField.setText("");
                    passwordField.setForeground(Color.BLACK);
                    passwordField.setEchoChar('*'); // 显示密码
                }
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                if (String.valueOf(passwordField.getPassword()).isEmpty()) {
                    passwordField.setText("请输入密码");
                    passwordField.setForeground(Color.GRAY);
                    passwordField.setEchoChar((char) 0); // 不显示密码
                }
            }
        });

        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(passwordIcon, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        inputPanel.add(passwordField, gbc);

        // 重复密码输入框
        JLabel repasswordIcon = new JLabel(new ImageIcon("resources/imgs/password.png")); // 替换为实际路径
        JPasswordField repasswordField = new JPasswordField("请输入重复密码", 20);
        repasswordField.setForeground(Color.GRAY);
        repasswordField.setEchoChar((char) 0); // 先不显示密码
        repasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                if (String.valueOf(repasswordField.getPassword()).equals("请输入重复密码")) {
                    repasswordField.setText("");
                    repasswordField.setForeground(Color.BLACK);
                    repasswordField.setEchoChar('*'); // 显示密码
                }
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                if (String.valueOf(repasswordField.getPassword()).isEmpty()) {
                    repasswordField.setText("请输入重复密码");
                    repasswordField.setForeground(Color.GRAY);
                    repasswordField.setEchoChar((char) 0); // 不显示密码
                }
            }
        });

        gbc.gridx = 0;
        gbc.gridy = 2;
        inputPanel.add(repasswordIcon, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        inputPanel.add(repasswordField, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton loginButton = new JButton("返回登录");

        // 设置按钮样式
        loginButton.setBackground(new Color(76, 175, 80)); // 设置背景色
        loginButton.setForeground(Color.WHITE); // 设置文字颜色
        loginButton.setFocusPainted(false); // 去掉焦点边框
        loginButton.setPreferredSize(new Dimension(100, 30)); // 设置按钮尺寸
        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                loginButton.setBackground(new Color(67, 160, 71)); // 悬停效果
            }

            @Override
            public void mouseExited(MouseEvent e) {
                loginButton.setBackground(new Color(76, 175, 80)); // 恢复原色
            }

            @Override
            public void mousePressed(MouseEvent e) {
                loginButton.setBackground(new Color(56, 142, 60)); // 点击效果
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                loginButton.setBackground(new Color(67, 160, 71)); // 恢复悬停效果
            }
        });

        JButton registerButton = new JButton("确认注册");
        registerButton.setBackground(new Color(33, 150, 243)); // 设置背景色
        registerButton.setForeground(Color.WHITE); // 设置文字颜色
        registerButton.setFocusPainted(false); // 去掉焦点边框
        registerButton.setPreferredSize(new Dimension(100, 30)); // 设置按钮尺寸
        registerButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                registerButton.setBackground(new Color(25, 135, 206)); // 悬停效果
            }

            @Override
            public void mouseExited(MouseEvent e) {
                registerButton.setBackground(new Color(33, 150, 243)); // 恢复原色
            }

            @Override
            public void mousePressed(MouseEvent e) {
                registerButton.setBackground(new Color(12, 97, 160)); // 点击效果
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                registerButton.setBackground(new Color(25, 135, 206)); // 恢复悬停效果
            }
        });

        // 绑定按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RegFrame.this.setVisible(false);
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 这里可以添加注册逻辑
                //获取用户输入的账号，密码以及重复密码
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                String repassword = new String(repasswordField.getPassword());
                if ("".equals(username) || "".equals(password) || "".equals(repassword)) {
                    JOptionPane.showMessageDialog(RegFrame.this, "请填写完整!");
                    return;
                }
                if (!password.equals(repassword)) {
                    JOptionPane.showMessageDialog(RegFrame.this, "两次密码不一致!");
                    return;
                }
                //执行注册代码
                String sql = "insert into user(username,password) values(?,?)";
                boolean b = DBUtils.exeUpdate(sql, username, password);
                if (b) {
                    JOptionPane.showMessageDialog(RegFrame.this, "注册成功");
                    RegFrame.this.dispose();
                } else {
                    JOptionPane.showMessageDialog(RegFrame.this, "注册失败");
                }
            }
        });

        // 添加按钮到按钮面板
        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);
        inputPanel.add(buttonPanel, gbc);

        // 版权信息面板
        JPanel copyrightPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel copyrightLabel = new JLabel("武汉工程大学邮电与信息工程学院");
        copyrightPanel.add(copyrightLabel);

        // 将面板添加到主面板
        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(inputPanel, BorderLayout.CENTER);
        mainPanel.add(copyrightPanel, BorderLayout.SOUTH);
        // 添加版权信息面板到底部

        // 将主面板添加到窗口
        add(mainPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            RegFrame frame = new RegFrame();
            frame.setVisible(true);
        });
    }
}
